package com.example.springboot.controller;

import com.example.springboot.common.Result;
import com.example.springboot.entity.SysMenu;
import com.example.springboot.entity.vo.MenuPageVO;
import com.example.springboot.service.DictService;
import com.example.springboot.service.MenuService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * @Author: jyf2000jyf
 * @Date: 2022/5/4
 * @Time: 23:03
 * @Email: 904593011@qq.com
 * @Description:
 **/
@RestController
@RequestMapping("menu")
public class MenuController {

    @Autowired
    MenuService menuService;

    @Autowired
    DictService dictService;

    // 新增和修改
    @PostMapping
    public Result save(@RequestBody SysMenu menu) {
        // 新增或者更新
        return Result.success(menuService.save(menu));
    }

    // 查询所有数据
    @GetMapping
    public Result index() {
        List<SysMenu> data = menuService.findAll();
        Integer total = menuService.getAmount("");
        return Result.success(new MenuPageVO(data, total));
    }

    @GetMapping("delete")
    public Result delete(@RequestParam Integer id) {
        menuService.delete(id);
        return Result.success(true);
    }

    @PostMapping("delete/batch")
    public Result deleteBatch(@RequestBody List<Integer> ids) {
        for (Integer id : ids) {
            menuService.delete(id);
        }
        return Result.success(true);
    }

    @GetMapping("page")
    public Result indexByPage(@RequestParam Integer pageNum,
                              @RequestParam Integer pageSize,
                              @RequestParam String name) {
        pageNum = (pageNum - 1) * pageSize;
        List<SysMenu> data = menuService.findAll(pageNum, pageSize, name);
        Integer total = menuService.getAmount(name);
        return Result.success(new MenuPageVO(data, total));
    }

    @GetMapping("icon")
    public Result getIcons(){
        return Result.success(dictService.getIcons());
    }
}
